package com.godyao.utils;

import com.godyao.framework.beans.factory.annotaions.Autowired;
import com.godyao.framework.stereotype.Component;
import com.godyao.framework.transaction.annotation.TxManager;

import java.sql.SQLException;

@Component("transactionManager")
public class TransactionManager implements TxManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 开启手动事务控制
     * @throws SQLException
     */
    @Override
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    /**
     * 提交事务
     * @throws SQLException
     */
    @Override
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }

    /**
     * 回滚事务
     * @throws SQLException
     */
    @Override
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
